#ifndef __POSTGRE_PROTOCOL_H__
#define __POSTGRE_PROTOCOL_H__

#include "base_protocol.h"



#define PG_PROTOCOL_VERSION_V3 3
#define PG_CMD_PACKET_HEADLEN 5
#define PG_NOCMD_PACKET_HEADLEN 4
#define PG_SSLREQUEST_BODY_LEN 4
#define PG_CANCELREQUEST_BODY_LEN 12


enum NoCmdReqStatus
{
    Unknown_cmd = -1,
    SSLRequest_cmd = 0,
    Strartup_cmd = 1,
    cancelRequest  = 3,
    LoadBalance_cmd = 4
};

enum SSLRequestStatus
{
    SSLError = 69,
    SSLRefused = 78,
    SSLOk = 83
};

//请求方向的消息类型
enum RequestType
{
    Strartup = 0,
    Bind = 66,
    Close = 67,
    Describe = 68,
    Execute = 69,
    FunctionCall = 70,  //VerifiedFiles vertica
    Flush = 72,
    Parse = 80,
    Query = 81,
    Sync = 83,
    Terminate = 88,
    Mars = 95,  // vertica
    CopyDoneRequest = 99,
    CopyDataRequest = 100,
    CopyErrorRequest = 101, //vertica
    CopyFail = 102,
    EndOfBatchRequest = 106,    //vertica
    PasswordMessage = 112
};

//返回方向的消息类型
enum RespondType
{
    ParseComplete = 49,
    BindComplete = 50,
    CloseComplete = 51,
    Notification = 65,
    BinaryRow = 66,
    CommandComplete = 67,
    DataRow = 68,
    Error = 69,
    VerifyFiles= 70,    //vertica
    CopyIn = 71,
    CopyOut = 72,   // LoadFile vertica
    EmptyQuery = 73,
    EndOfBatchResponse = 74,    // vertica
    BackendKeyData = 75,
    Notice = 78,
    WriteFile = 79, //vertica
    PortalName = 80,
    Auth = 82,
    ParameterStatus = 83,
    RowDescription = 84,
    FunctionCallResponse = 86,
    CopyBoth = 87,
    LoadBalance = 89,   //vertica
    ReadForQuery = 90,
    MarsResponse = 95,  //vertica
    CopyDoneResponse = 99,
    CpoyDataResponse = 100,
    CommandDescription = 109,   //vertica
    NoData = 110,
    PortalSuspended = 115,
    ParameterDescription = 116
};


namespace secsmart_protocol
{
    class PostgreProtocol : public BaseProtocol
    {
        public:
            PostgreProtocol()
            {
                databaseName = DBNAME_POSTGRE;
            }

            virtual DETECT_RESULT parse(secsmart_session::session_sptr session, const uint8_t *buf, size_t size, bool is_forward);
            std::string extendInfo();

        private:
            bool isTrueV3Cmd(int8_t cmdType);


        private:
            NoCmdReqStatus m_noCmdReqStatus = NoCmdReqStatus::Unknown_cmd;
            
    };
    
}

#endif










